package application.client.gui.components;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import middleware.datacontainer.searchclient.ClientContainer;
import middleware.datacontainer.searchclient.ExemplarContainer;

import application.client.gui.interfaces.IChoosable;
import application.controller.client.GuiController;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ClientSearchFrame extends JFrame {
	private JPanel clientSearchPanel;
	private JLabel dummySelectClientLabel;
	private JButton selectClientButton;
	private JScrollPane ClientResultScrollPane;
	private JTable ClientResultTable;
	private JTextField searchField;
	private JButton searchButton;
	private String[][] clientSearchResult=new String[][] {  };
	private String[] clientSearchTableHeader=new String[] { "First Name 1", "Last Name" };
	private Integer selectedRowIndexClientTable;
	private IChoosable clientChooser;
	private boolean isItALoan;
	
	private HashMap<Integer,ClientContainer> clients = new HashMap<Integer,ClientContainer>();

	public ClientSearchFrame(IChoosable pClientChooser, boolean pIsItALoan)
	{
		super();
		setLocation(new Point(250,150));
		isItALoan=pIsItALoan;
		clientChooser=pClientChooser;
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				this.setVisible(true);
				this.setPreferredSize(new java.awt.Dimension(300, 400));
				{
					clientSearchPanel = new JPanel();
					getContentPane().add(clientSearchPanel, BorderLayout.CENTER);
					clientSearchPanel.setBackground(new java.awt.Color(255,255,255));
					{
						searchField = new JTextField();
						clientSearchPanel.add(searchField);
						searchField.setVisible(true);
						searchField.setSize(210, 23);
						searchField.setPreferredSize(new java.awt.Dimension(225, 23));

					}
					{							
						searchButton = new JButton();
						clientSearchPanel.add(searchButton);
						searchButton.setVisible(true);
						searchButton.setText("Go!");
						searchButton.addActionListener(new ActionListener()
						{
							
							@Override
							public void actionPerformed(ActionEvent e) {
								Object source=e.getSource();
								if(searchField.getText()!="")
								{									
									setClientSearchResult(
											GuiController.getInstance().searchClient(searchField.getText()));
								}									
							}
							
						});
					}
					{
						ClientResultScrollPane = new JScrollPane();
						clientSearchPanel.add(ClientResultScrollPane);
						ClientResultScrollPane.setPreferredSize(new java.awt.Dimension(264, 282));
						{
							TableModel ClientResultTableModel = 
									new DefaultTableModel(
											clientSearchResult,
											clientSearchTableHeader);
							ClientResultTable = new JTable();
							ClientResultScrollPane.setViewportView(ClientResultTable);
							ClientResultTable.setModel(ClientResultTableModel);
							ClientResultTable.setPreferredSize(new java.awt.Dimension(400, 305));
						}
					}
					{
						dummySelectClientLabel = new JLabel();
						clientSearchPanel.add(dummySelectClientLabel);
						dummySelectClientLabel.setPreferredSize(new java.awt.Dimension(180, 30));
					}
					{
						selectClientButton = new JButton();
						clientSearchPanel.add(selectClientButton);
						selectClientButton.setText("select Client");
						selectClientButton.addActionListener(new ActionListener()
						{

							@Override
							public void actionPerformed(ActionEvent arg0) {
								
								setInVisible();
								
									if(!isItALoan)
									{
										System.out.println("reserve!" + clients.get(ClientResultTable.getSelectedRow()).getId());
										clientChooser.reserveMedium(clients.get(ClientResultTable.getSelectedRow()));
										
									}
									else
									{
										System.out.println("bin da");
										if(ClientResultTable.getSelectedRow()>=0)
										{
											System.out.println("bin drin");
											clientChooser.loan(clients.get(ClientResultTable.getSelectedRow()));
										}					
									
									}
								close();
							}
							
						});
					}
				}
				setDefaultCloseOperation(HIDE_ON_CLOSE);
				this.setSize(300, 400);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	public void setClientSearchResult(List<ClientContainer> result)
	{
		clientSearchResult = new String[result.size()][clientSearchTableHeader.length-1];
		TableModel clientsTableModel=new DefaultTableModel(clientSearchResult, clientSearchTableHeader);
				
		int row = 0;
		for (ClientContainer cc : result) {
			
			if(cc != null)
			{
				clientsTableModel.setValueAt(cc.getFname(), row, 0);
				clientsTableModel.setValueAt(cc.getLname(), row, 1);
				clients.put(row, cc);
				row++;
			}
		}
		ClientResultTable.setModel(clientsTableModel);
		ClientResultTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){

			@Override
			public void valueChanged(ListSelectionEvent e) {
				  if (!e.getValueIsAdjusting()) {
					  selectedRowIndexClientTable=ClientResultTable.getSelectedRow();
				  }
			}
		
		});
		
	}
	private void close()
	{
		System.out.println("clientsearchframeclosed!");
		this.setVisible(false);
		this.dispose();
	}
	private void setInVisible()
	{
		this.setVisible(false);
	}
}
